package com.xcx.eduservice.controller.aclcontroller;


import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import com.xcx.eduservice.entity.aclentity.Permission;
import com.xcx.eduservice.request.aclrequest.PromissionRequest;
import com.xcx.eduservice.request.aclrequest.RolePromissionLink;
import com.xcx.eduservice.request.aclrequest.userrequest.UserGetProcessionRequest;
import com.xcx.eduservice.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限 菜单管理
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Api(tags = "权限管理系统")
@RestController
@RequestMapping("/acl/permission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin//解决跨域问题
public class PermissionController {


    final private PermissionService permissionService;

    //获取全部菜单
    @ApiOperation(value = "查询所有菜单")
    @GetMapping("getpremisson")
    public ReturnMessage<List<Permission>> indexAllPermission() {
        List<Permission> list = permissionService.queryAllMenuGuli();
        return ReturnMessageUtils.success(list);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public ReturnMessage<List<Permission>> remove(@ApiParam(name = "id", value = "菜单id", required = false,example = "11")
                                                  @PathVariable String id) {
        permissionService.removeChildByIdGuli(id);
        return ReturnMessageUtils.success();
    }

    @ApiOperation(value = "给角色分配菜单")
    @PostMapping("/doAssign")
    public ReturnMessage<Object> doAssign(@RequestBody RolePromissionLink rolePromissionLink) {
        String roleId = rolePromissionLink.getRoleId();
        List<String> permissionIds = rolePromissionLink.getPermissionIds();
        permissionService.saveRolePermissionRealtionShipGuli(roleId, permissionIds);
        return ReturnMessageUtils.success();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public ReturnMessage<Object> toAssign(@ApiParam(name = "roleId", value = "角色的id", required = false,example = "1")
            @PathVariable String roleId) {
        List<Permission> list = permissionService.selectAllMenu(roleId);
        return ReturnMessageUtils.success(list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public ReturnMessage<Permission> save(@RequestBody PromissionRequest promissionRequest) {

        return permissionService.savePromission(promissionRequest);
    }




    @ApiOperation(value = "根据用户id查询用户的所有权限")
    @PostMapping("getUserPermission")
    public  ReturnMessage<List<Permission>> getUserPermission(@RequestBody UserGetProcessionRequest userGetProcessionRequet) {

        return permissionService.getPromissByUserId(userGetProcessionRequet);
    }












//
//    @ApiOperation(value = "修改菜单")
//    @PutMapping("update")
//    public ReturnMessage<Permission> updateById(@RequestBody Permission permission) {
//        permissionService.updateById(permission);
//        return ReturnMessageUtils.success();
//    }

}

